<?php
/**
 * 
 * The Digitalus core serves as a registry for the CMS Resources
 *
 * @package    	Digitalus\Core
 * @copyright  	Copyright (c) 2009,  Digitalus Media USA (http://digitalusmedia.net)
 * @license    	New BSD License, library/Dig/License.txt
 * @version 	2.0
 * 
 */
class Dig_Service_Core
{
    const REGISTRY_KEY = 'digitalus_core';
    public $params = array();

    /**
     * registers a value with the core
     *
     * @example Dig_Service_Core::setParam('some_array', array('value', ...));
     * 
     * @param string $key A unique string identifier
     * @param mixed $value The core can store any type of data
     */
    public static function setParam ($key, $value)
    {
        $core = self::getInstance();
        $core->params[$key] = $value;
        $core->save();
    }

    /**
     * Tests to see if a parameter has been set
     *
     * @example Dig_Service_Core::hasParam('some_array')
     *
     * @param string $key A unique string identifier
     * @return boolean
     */
    public static function hasParam($key)
    {
        $core = self::getInstance();
        if(isset($core->params[$key])) {
            return true;
        }
        return false;
    }

    /**
     * Fetches a parameter from the core
     *
     * @example $someArray = Dig_Service_Core::getParam('some_array');
     *
     * @param string $key A unique string identifier
     * @return mixed
     */
    public static function getParam ($key)
    {
        $core = self::getInstance();
        return $core->params[$key];
    }
    /**
     * Gets the current Dig core instance
     *
     * @example $digCore = Dig_Service_Core::getInstance();
     * 
     * @return Dig_Service_Core
     */
    static function getInstance ()
    {
        if (! Zend_Registry::isRegistered(self::REGISTRY_KEY)) {
            Zend_Registry::set(self::REGISTRY_KEY, new self());
        }
        return Zend_Registry::get(self::REGISTRY_KEY);
    }

    /**
     * Saves the core to Zend Registry
     *
     * This is done automatically by the setParam method and only needs to be explicitly
     * called if you fetch the core instance and manipulate it directly.
     * 
     */
    public function save ()
    {
        Zend_Registry::set(self::REGISTRY_KEY, $this);
    }

    /**
     * Handles the setter and getter methods for the params
     *
     * This enables you to use magic getter and setter methods for a more fluid interface.
     *
     * @example $acl = Dig_Service_Core::getInstance()->getAcl();
     *
     * @param string $name
     * @param array $arguments
     */
    public function __call ($name, $arguments)
    {
        $magicMethods = array('get' , 'set');
        foreach ($magicMethods as $method) {
            if (substr($name, 0, strlen($method)) == $method) {
                $methodObject = new stdClass();
                $methodObject->method = $method;
                $methodObject->param = strtolower(Zend_Filter::filterStatic(str_replace($method, '', $name), 'Word_CamelCaseToUnderscore'));
            }
        }
        if ($methodObject) {
            switch ($methodObject->method) {
                case 'get':
                    return self::getParam($methodObject->param);
                case 'set':
                    $value = $arguments[0];
                    return self::setParam($methodObject->param, $value);
            }
        }
    }
}
?>
